#include "ant_worker.hh"
#include "ant_queen.hh"

Worker::Worker (int x, int y):
  Ants ()
{
  pos_.first = x;
  pos_.second = y;
}

Worker::~Worker ()
{
}

bool
Worker::is_near_queen ()
{
  Map *m = Map::instance ();
  int team = get_team ();

  if ((dynamic_cast<Queen*> (&(m->get_case (pos_.second - 1, pos_.first - 1).get_ant ())) &&
       team == m->get_case (pos_.second - 1, pos_.first - 1).get_ant ().get_team ()) ||
      (dynamic_cast<Queen*> (&(m->get_case (pos_.second - 1, pos_.first).get_ant ())) &&
       team == m->get_case (pos_.second - 1, pos_.first).get_ant ().get_team ()) ||
      (dynamic_cast<Queen*> (&(m->get_case (pos_.second - 1, pos_.first + 1).get_ant ())) &&
       team == m->get_case (pos_.second - 1, pos_.first + 1).get_ant ().get_team ()) ||
      (dynamic_cast<Queen*> (&(m->get_case (pos_.second, pos_.first - 1).get_ant ())) &&
       team == m->get_case (pos_.second, pos_.first - 1).get_ant ().get_team ()) ||
      (dynamic_cast<Queen*> (&(m->get_case (pos_.second, pos_.first + 1).get_ant ())) &&
       team == m->get_case (pos_.second, pos_.first + 1).get_ant ().get_team ()) ||
      (dynamic_cast<Queen*> (&(m->get_case (pos_.second + 1, pos_.first - 1).get_ant ())) &&
       team == m->get_case (pos_.second + 1, pos_.first - 1).get_ant ().get_team ()) ||
      (dynamic_cast<Queen*> (&(m->get_case (pos_.second + 1, pos_.first).get_ant ())) &&
       team == m->get_case (pos_.second + 1, pos_.first).get_ant ().get_team ()) ||
      (dynamic_cast<Queen*> (&(m->get_case (pos_.second + 1, pos_.first + 1).get_ant ())) &&
       team == m->get_case (pos_.second + 1, pos_.first + 1).get_ant ().get_team ()))
    return true;
  else
    return false;
}

bool
Worker::is_near_food ()
{
  Map *m = Map::instance ();

  if ((m->get_case (pos_.second - 1, pos_.first - 1).get_food () > 0) ||
      (m->get_case (pos_.second - 1, pos_.first).get_food () > 0) ||
      (m->get_case (pos_.second - 1, pos_.first + 1).get_food () > 0) ||
      (m->get_case (pos_.second, pos_.first + 1).get_food () > 0) ||
      (m->get_case (pos_.second, pos_.first - 1).get_food () > 0) ||
      (m->get_case (pos_.second + 1, pos_.first - 1).get_food () > 0) ||
      (m->get_case (pos_.second + 1, pos_.first).get_food () > 0) ||
      (m->get_case (pos_.second + 1, pos_.first + 1).get_food () > 0))
    return true;
  return false;
}

void
Worker::get_food ()
{

}

void
Worker::give_food ()
{

}

bool
Worker::got_food ()
{
  //
  // FIXME
  //
  return false;
}

void
Worker::print ()
{
  if (food_ > 0)
    std::cout << P_FOOD_ANT;
  else
    std::cout << P_FOOD_ANT_EMPTY;
}

void
Worker::get_next_step ()
{
//   int retry = 0;
//   std::pair<int, int> *c;
//   Map *m = Map::instance ();
//   Case cell;

//   for (retry = 0; retry < 10; retry++)
//   {
//     c = get_rand_case ();
//     cell = m->get_case (c->second, c->first);
//     if (cell.is_empty ()) // et sup egal a l'interet
//     {
//       //
//       // FIXME
//       //

//     }
//   }
}


/*!
** @brief highest ph score between the 8 near cells minus pb enemies
**
*/
void
Worker::interest_scoring ()
{
  if (food_ > 0)
  {
    max_score_ = interest_scoring_queen ();
  }
  else
  {
    max_score_ = interest_scoring_food ();
  }
}

int
Worker::interest_scoring_queen ()
{
  Map *m = Map::instance ();

  int tmp = 0;
  tmp = m->get_case (pos_.second - 1, pos_.first - 1).get_interest_queen (team_);
  if (tmp > max_score_)
    max_score_ = tmp;
  else
  {
    tmp = m->get_case (pos_.second - 1, pos_.first).get_interest_queen (team_);
    if (tmp > max_score_)
      max_score_ = tmp;
    else
    {
      tmp = m->get_case (pos_.second - 1, pos_.first + 1).get_interest_queen (team_);
      if (tmp > max_score_)
	max_score_ = tmp;
      else
      {
	tmp = m->get_case (pos_.second, pos_.first - 1).get_interest_queen (team_);
	if (tmp > max_score_)
	  max_score_ = tmp;
	else
	{
	  tmp = m->get_case (pos_.second, pos_.first + 1).get_interest_queen (team_);
	  if (tmp > max_score_)
	    max_score_ = tmp;
	  else
	  {
	    tmp = m->get_case (pos_.second + 1, pos_.first - 1).get_interest_queen (team_);
	    if (tmp > max_score_)
	      max_score_ = tmp;
	    else
	    {
	      tmp = m->get_case (pos_.second + 1, pos_.first).get_interest_queen (team_);
	      if (tmp > max_score_)
		max_score_ = tmp;
	      else
	      {
		tmp = m->get_case (pos_.second + 1, pos_.first + 1).get_interest_queen (team_);
		if (tmp > max_score_)
		  max_score_ = tmp;
	      }
	    }
	  }
	}
      }
    }
  }
  return tmp;
}

int
Worker::interest_scoring_food ()
{
  Map *m = Map::instance ();

  int tmp = 0;
  tmp = m->get_case (pos_.second - 1, pos_.first - 1).get_interest_food (team_);
  if (tmp > max_score_)
    max_score_ = tmp;
  else
  {
    tmp = m->get_case (pos_.second - 1, pos_.first).get_interest_food (team_);
    if (tmp > max_score_)
      max_score_ = tmp;
    else
    {
      tmp = m->get_case (pos_.second - 1, pos_.first + 1).get_interest_food (team_);
      if (tmp > max_score_)
	max_score_ = tmp;
      else
      {
	tmp = m->get_case (pos_.second, pos_.first - 1).get_interest_food (team_);
	if (tmp > max_score_)
	  max_score_ = tmp;
	else
	{
	  tmp = m->get_case (pos_.second, pos_.first + 1).get_interest_food (team_);
	  if (tmp > max_score_)
	    max_score_ = tmp;
	  else
	  {
	    tmp = m->get_case (pos_.second + 1, pos_.first - 1).get_interest_food (team_);
	    if (tmp > max_score_)
	      max_score_ = tmp;
	    else
	    {
	      tmp = m->get_case (pos_.second + 1, pos_.first).get_interest_food (team_);
	      if (tmp > max_score_)
		max_score_ = tmp;
	      else
	      {
		tmp = m->get_case (pos_.second + 1, pos_.first + 1).get_interest_food (team_);
		if (tmp > max_score_)
		  max_score_ = tmp;
	      }
	    }
	  }
	}
      }
    }
  }
  return tmp;
}

//!
// @brief if near food & no food : get food - drop 5 PF - exit
// if near queen & got food : give food (Q+1) - drop 5 PQ - exit
// else if random == highest interest cell : move to it. (10 times)
// if new cell is phed : drop 1 same ph
//
void
Worker::move ()
{

}
